app.controller('statusCtrl', function ($rootScope,$scope, $http, $interval, URLService) {
    //通用配置
    var options={
        schema:{
            model:{
                id: "itemId",
                parentId: "parentId",
                fields: {
                    itemId: {type: "number", nullable: true},
                    parentId: {nullable: true, type: "number"}
                },
                expanded:true
            }
        }
    };
    $scope.options = {
        //服务状态表格配置
        treelistOptions: {
            height:'100%',
            resizable: true,
            messages: {
                noRows: $scope.I18N.NORECORDS
            },
            columnMenu: false,
            columns: [
                {field: "itemId", title: " ", width: "15px",template:function () {
                    return '<div class="icon-btn"></div>';
                }},
                {field: "key", title: $scope.i18n.serverName, width: "150px"},
                {field: "value", title: $scope.i18n.serverIp, width: "150px"},
                {field: "comment", title: $scope.i18n.description, width: "150px"},
                {field: "status", title: $scope.i18n.status, width: "150px",template:function (dataItem) {
                    if(angular.isUndefined(dataItem.status)){
                        return '';
                    }else{
                        return dataItem.status?$scope.i18n.start:'<span class="server-stop">'+$scope.i18n.stop+'</span>';
                    }

                }}
            ],
            dataSource: {
                transport: {
                    read: _readServerStatus,
                    parameterMap: function (options, operation) {
                        // if (operation !== "read" && options.models) {
                        //     return {models: kendo.stringify(options.models)};
                        // }
                    }
                },
                schema: options.schema
            },
            dataBound:_dataBoundFun
        }
    };

    //通过该方式初始化的kendoTreeList控件每次展开/收起子项不会改变表格DOM，可实现子项样式修改
    $('#serverStatusGrid').kendoTreeList(
        $scope.options.treelistOptions
    );

    //轮询 更新报表
    $rootScope.serverStatusTimer= $interval(updateServerStatus,30*1000);

    //更新服务状态
    function updateServerStatus() {
        kendo.ui.progress(angular.element('.loading'), true);
        var url = URLService.getURL('serverStatusInfo');
        $http.post(url).success(function (response) {
            kendo.ui.progress(angular.element('.loading'), false);
            var grid=$('#serverStatusGrid').data('kendoTreeList');
            if (response.success) {
                if(grid){
                    grid.setDataSource({
                        data:response.data,
                        schema: options.schema
                    });
                }
            } else {
                if(grid){
                    grid.setDataSource([]);
                    Ut.dialog.errMsg(response.errorMsg);
                }
            }
        });
    }

    //读取服务状态
    function _readServerStatus(options) {
        kendo.ui.progress(angular.element('.loading'), true);
        var url = URLService.getURL('serverStatusInfo');
        $http.post(url).success(function (response) {
            kendo.ui.progress(angular.element('.loading'), false);
            if (response.success) {
                options.success(response.data);
            } else {
                options.success([]);
                Ut.dialog.errMsg(response.errorMsg);
            }
        });
    }

    function _dataBoundFun (e) {
        var widget = e.sender;
        widget.wrapper.find(".icon-btn").each(function(_, wrapper) {
            var td = $(wrapper).parent();
            var level = td.find(".k-icon").length;
            if(level===2){
                //子行
                var tr = td.parent();
                //展开后子行tr的class和style都会被更新，故添加特殊属性，方便样式选择器定位
                $(tr).attr('child-item',true);
            }
        });

    }
});
